package com.shiliao.app.activity.index.presenter;

import com.shiliao.app.activity.index.service.IIndexService;
import com.shiliao.app.activity.index.service.impl.IndexImpl;
import com.shiliao.app.activity.index.view.MainIndexView;
import com.shiliao.app.event.MessageEvent;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.YMServerUtil;
import com.yimei.framework.mvp.presenter.IBasePresenter;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/9/12.
 */

public class IndexPresenter extends IBasePresenter<IIndexService> {
    private MainIndexView mainIndexView;
    private IIndexService iIndexService;

    public IndexPresenter(MainIndexView mainIndexView) {
        this.mainIndexView = mainIndexView;
        iIndexService = new IndexImpl();
        setService(iIndexService);
    }

    public void request(String device, String phone) {

        //原来获得 userid 的代码移到 welcomeactivity 中，这里只 checkmessage
        String local_userId = "0";

        local_userId = mainIndexView.getUserId();
        if (!local_userId.equals("0")) {
            checkMessage(local_userId);
        }
    }

    private void checkMessage(String userId) {
        Map param = new HashMap();
        //  JSONObject param=new JSONObject();

        param.put("user_id", userId);

        iIndexService.request(YMServerUtil.CHECK_MESSAGE, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                String retcode = ParseHelper.getString(result.toString(), "new_msg_no");
                if (!retcode.equals("0")) {
                    YMUserManager.bHasnewmessage = true;
                    MessageEvent mEvent = new MessageEvent();
                    mEvent.eventId = MessageEvent.EVENT_ID_NEW_MSG_STATUS_CHANGE;
                    EventBus.getDefault().post(mEvent);
                }
            }

            @Override
            public void onFail(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
            }
        });
    }
}
